﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using Sissonix.ExtRest.RestManagerModel.Model;
using Sissonix.Infrastructure;

namespace Sissonix.ExtRest.RestManagerModel.Repository
{
    class UserRepository : EFRepositoryBase<User>, IUserRepository
    {
        public UserRepository (UnitOfWork uow) : base(uow) {}
        public UserRepository() : base() { }

        #region IRepository<User> Members
        public override User FindBy(object key)
        {
            long lKey = Int64.Parse(key.ToString());
            RestManagerEntities entities = GetEFContext();
            ObjectQuery<User> User = GetEFContext().User;

            IQueryable<User> query =
                from c in User
                where c.UserID == lKey
                select c;

            return query.First();
        }

        public override IList<User> FindAll()
        {
            return FindAll(0, 0, null, null);
        }

        public override int Count()
        {
            RestManagerEntities entities = GetEFContext();
            var users = entities.User;
            return users.Count();
        }

        public override IList<User> FindAll(int? start, int? limit, string sortField, string sortDirection)
        {
            if (sortField == "") sortField = "UserID";
            if (!start.HasValue) start = 0;
            if (!limit.HasValue) limit = int.MaxValue;

            RestManagerEntities entities = GetEFContext();
            ObjectQuery<User> qry = entities.User;

            if (sortField != "")
                qry = qry.OrderBy("it." + sortField + " " + sortDirection);

            if (start.HasValue && limit.HasValue)
            {
                qry = (ObjectQuery<User>)qry.Skip(start.Value);
                qry = (ObjectQuery<User>)qry.Take(limit.Value);
            }

            return qry.Execute(MergeOption.AppendOnly).ToList<User>();
        }
        #endregion

        #region RepositoryBase abstract Members
        protected override void EFPersistNewItem(User item)
        {
            persistenceContext.AddObject("User", item);
        }
        protected override void EFPersistUpdatedItem(User item)
        {
            //This is very discouraging. In EF, you have to load the record
            //from the database first in order to update it.  What a waste!
            try
            {
                persistenceContext.Connection.Open();
                var rec = (from u in persistenceContext.User where u.UserID == item.UserID select u)
                    .FirstOrDefault();
                if (rec != null)
                {
                    persistenceContext.ApplyPropertyChanges("User", item);
                }
            }
            catch (Exception e)
            {
            }
            return; 
        }
        protected override void EFPersistDeletedItem(User item)
        {
            persistenceContext.DeleteObject(item);
        }
        #endregion
    }
}
